//
//  OneToMoreViewController.m
//  EnjoyArkUIXIOS
//
//  Created by Dordly on 2024/1/30.
//

#import "OneToMoreViewController.h"
#import "MainTableViewCell.h"
#import "BaseModuleViewController.h"
#import "PublicAlertView.h"
#import <libarkui_ios/StageApplication.h>
#import <libarkui_ios/BridgePlugin.h>
#import "PublicHeader.h"

static NSString *const CELL = @"OneToMoreMainTableViewCell";

@interface OneToMoreViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *dataArray;

@property (nonatomic, strong) PublicAlertView *publicAlertView;

@end

@implementation OneToMoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self setNavigationBarWithCenterTitle:self.titleName];
    [self.view addSubview:self.mainTableView];
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    [self.mainTableView registerClass:[MainTableViewCell class] forCellReuseIdentifier:CELL];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    appDelegate.allowRotation = NO;
    [self setNewOrientation:NO];
}

- (void)setIsPortrait:(BOOL)isPortrait {
    if (!self.mainTableView) {
        return;
    }
    CGFloat width = self.view.frame.size.width;
    CGFloat height = self.view.frame.size.height;
    if ((isPortrait && height < width) || (!isPortrait && width < height)) {
        width = self.view.frame.size.height;
        height = self.view.frame.size.width;
    }
    if (self.mainTableView.frame.size.width == width) {
        return;
    }
    self.mainTableView.frame = self.view.frame;
    [self.mainTableView reloadData];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL forIndexPath:indexPath];
    if (!cell) {
        cell = [[MainTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CELL];
    }
    cell.titleName = [self.dataArray[indexPath.row] objectForKey:@"name"];
    cell.publicWidth = self.view.frame.size.width;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *moduleNameStr = [self.dataArray[indexPath.row] objectForKey:@"moduleName"];
    NSString *titleName = [self.dataArray[indexPath.row] objectForKey:@"name"];
    NSString *abilityName = [self.dataArray[indexPath.row] objectForKey:@"abilityName"];
    if (moduleNameStr.length <= 0) {
        [self.publicAlertView showView];
        return;
    }
    BaseModuleViewController *baseModuleVC = [[BaseModuleViewController alloc] init];
    if (self.pageType == Page_Type_C || self.pageType == Page_Type_version) {
        baseModuleVC.isShowNavigationBar = YES;
    } else {
        baseModuleVC.isShowNavigationBar = NO;
    }
    baseModuleVC.titleName = titleName;
    baseModuleVC.instanceName = [NSString stringWithFormat:@"%@:%@:%@", BUNDLE_NAME, moduleNameStr, abilityName];
    baseModuleVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:baseModuleVC animated:YES];
}


- (PublicAlertView *)publicAlertView {
    if (!_publicAlertView) {
        _publicAlertView = [[PublicAlertView alloc] initWithFrame:self.view.frame mainTitle:@"待开发中……"];
    }
    return _publicAlertView;
}

- (NSArray *)dataArray {
    if (self.pageType == Page_Type_version) {
        return @[@{@"name" : @"特性一", @"moduleName" : @"", @"abilityName" : @""},
                 @{@"name" : @"特性二", @"moduleName" : @"", @"abilityName" : @""}];
    }
    if (self.pageType == Page_Type_C) {
        return @[@{@"name" : @"C ～ Helloworld", @"moduleName" : @"napi_helloword", @"abilityName" : @"Napi_hellowordAbility"},
                 @{@"name" : @"C++ ~ OpenCV图片处理", @"moduleName" : @"opencvmodule", @"abilityName" : @"OpenCVModuleAbility"}];
    }
    return @[@{@"name" : @"音乐播放器", @"moduleName" : @"musicplayer", @"abilityName" : @"MusicPlayerAbility"},
             @{@"name" : @"应用市场首页", @"moduleName" : @"appstorefeature", @"abilityName" : @"AppstorefeatureAbility"}];
}

@end
